package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.GroupDAO;
import entity.Group;
import entity.User;
import groupInformation.Announcement;
import groupInformation.Task;
import lombok.SneakyThrows;
import serviceImpl.GainTeam;
import serviceImpl.UpdateTeam;
import serviceImpl.updateUserData;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "GroupServlet", value = "/GroupServlet")
public class GroupServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out=response.getWriter();


        String name=request.getParameter("name");
        String profile=request.getParameter("profile");
        String announcement=request.getParameter("announcement");
        String task1=request.getParameter("task1");
        String task2=request.getParameter("task2");
        String task3=request.getParameter("task3");
        StringBuilder sb=new StringBuilder();
        sb.append(task1).append(",").append(task2).append(",").append(task3);
        String task=sb.toString();
        System.out.println("获取到的:"+task);
        User user =(User) request.getSession().getAttribute("user");
        GainTeam gain=new GainTeam();
        Group group = gain.GainTeam(user);
        UpdateTeam up=new UpdateTeam();

        group.setName(name);
        group.setProfile(profile);
        boolean t = up.updateTeamData(group);
        if(t==true){
            Announcement an=new Announcement();
            an.setContent(announcement);
            an.setGroup_id(""+group.getId());
            an.setSponsor(user.getId()+"");
            GroupDAO.announcement(an);
            GroupDAO.updateTask(group.getId(),task);

        }

        ObjectMapper mapper = new ObjectMapper();
        String json =  mapper.writeValueAsString(group);
      //  System.out.println(json);
        out.print(json);
    }
}
